package com.meet.controller;

import com.meet.dao.MeetingDao;
import com.meet.dao.MeetingDaoImpl;
import com.meet.dao.WorkersDao;
import com.meet.dao.MeetingDaoImpl;
import com.meet.dao.WorkersDaoImpl;
import com.meet.entity.ConnectionByWM;
import com.meet.entity.Meeting;
import com.meet.entity.PageBean;
import com.meet.entity.Workers;
import com.meet.service.ConnectionService;
import com.meet.service.ConnectionServiceImpl;
import com.meet.service.ConnectionServiceImpl;
import com.meet.vo.ConnectionVo;
import com.meet.vo.MeetingVo;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author King_of_hehe
 */
@WebServlet(name = "ConnectionServlet", value = "/ConnectionServlet")
public class ConnectionServlet extends HttpServlet {

    private ConnectionService connectionService = new ConnectionServiceImpl();
    private MeetingDao meetingDao = new MeetingDaoImpl();
    private WorkersDao workersDao = new WorkersDaoImpl();


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag = request.getParameter("flag");

        switch (flag){
            case "1"://添加连接
                add(request, response);
                break;
            case "2"://删除连接
                drop(request, response);
                break;
            case "3"://修改连接第一步
                editOne(request, response);
                break;
            case "4"://修改连接第二步
                editTwo(request, response);
                break;
            case "5"://分页vo连接数据
                listVo(request, response);
                break;
            case "6"://转换id至名
                getData(request, response);
                break;
            case "7"://修改确认状态
                yes(request,response);
            default://报错
                break;

        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    /** 添加数据
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

        int rId = Integer.parseInt(request.getParameter("rId"));
        int wId = Integer.parseInt(request.getParameter("wId"));

        ConnectionByWM wm = new ConnectionByWM(rId,wId);
        try {
            connectionService.addConnection(wm);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        response.sendRedirect("ConnectionServlet?flag=5");
    }

    /** 删除连接
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void drop(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

    }

    /** 第一步取出数据
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void editOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

    }

    /** 第二步修改数据
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void editTwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

    }

    /** 获取vo数据
     *-*---------------55-----
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void listVo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String currentPage = request.getParameter("page");//当前页码
        String rows = request.getParameter("rows");//每页显示条数
        //得到session的id
        HttpSession session= request.getSession();
        Workers worker= (Workers) session.getAttribute("worker");
        int id=worker.getId();
       // int id=1;
        if (currentPage == null || "".equals(currentPage)) {

            currentPage = "1";
        }

        if (rows == null || "".equals(rows)) {
            rows = "10";
        }
        PageBean<ConnectionVo> pb = null;

        try {
            pb = connectionService.getAllWM(Integer.parseInt(currentPage),Integer.parseInt(rows),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //3.将PageBean存入request
        request.setAttribute("pb", pb);

        //4.转发到main.jsp
        request.getRequestDispatcher("connectionShow.jsp").forward(request, response);
    }

    private void getData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        List<Meeting> meetingList = null;
        List<Workers> workersList = null;

        try {
            meetingList = meetingDao.getAll();
            workersList = workersDao.getAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        request.setAttribute("meet",meetingList);
        request.setAttribute("worker",workersList);

        request.getRequestDispatcher("connectionAdd.jsp").forward(request, response);
    }

    private void yes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        int id = Integer.parseInt(request.getParameter("id"));
        try {
            connectionService.yes(id);
            //成功后跳转
            response.sendRedirect("ConnectionServlet?flag=5");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
